package jmine.tec.web.wicket.pages.example.form;

import static jmine.tec.web.wicket.pages.form.WebFormPagesMessages.INFO_ENTITY_SAVE_SUCCESS;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import jmine.tec.web.wicket.component.autocomplete.CustomAutocompleteTextField;
import jmine.tec.web.wicket.component.autocomplete.NaturalKeyAutoCompleteTextRenderer;
import jmine.tec.web.wicket.component.autocomplete.NaturalKeyChoicesLoader;
import jmine.tec.web.wicket.dao.WebTestEntityDAO;
import jmine.tec.web.wicket.domain.WebTestEntity;
import jmine.tec.web.wicket.mock.MockMessageCreator;
import jmine.tec.web.wicket.pages.form.FormPage;
import jmine.tec.web.wicket.pages.form.FormType;
import jmine.tec.web.wicket.pages.validator.DateIntervalFormValidator;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.validation.IFormValidator;

import bancosys.tec.exception.MessageCreator;

/**
 * Exemplo de um formulario de cadastro.
 * 
 * @author Rafael Volpato
 * @date Nov 10, 2011
 */
public class FormPageExample extends FormPage<WebTestEntity> {
    
    public static final int MAX_DATE_SPAN = DateIntervalFormValidator.Interval.WEEK.getDays(); 
    
    FormComponent<java.util.Date> dataComponent;

    FormComponent<java.util.Date> dataFimComponent;
    
    public FormPageExample(PageParameters pageParameters) {
        super(pageParameters);
    }

    public FormPageExample(Page sourcePage, PageParameters sourcePageParameters, WebTestEntity entity, FormType formType) {
        super(sourcePage, sourcePageParameters, entity, formType);
    }

    public FormPageExample(PageParameters pageParameters, Page pageInstance) {
        super(pageParameters, pageInstance);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected List<Component> createFormComponents() {
        List<Component> components = new ArrayList<Component>();

        components.add(this.createTextField("pk"));
        components.add(this.createTextField("nome"));
        components.add(this.createTextField("mnemonico"));
        components.add(this.createTextField("valor"));
        components.add(this.dataComponent = this.createDateField("data").setRequired(true));
        components.add(this.dataFimComponent = this.createDateField("dataFim").setRequired(true));

        components.add(new CustomAutocompleteTextField<WebTestEntity>("parent", new NaturalKeyChoicesLoader<WebTestEntity>(
                WebTestEntity.class), NaturalKeyAutoCompleteTextRenderer.<WebTestEntity> instance()));

        return components;
    }

    @Override
    protected List<IFormValidator> createFormValidators() {
        return Collections.<IFormValidator>singletonList(
                new DateIntervalFormValidator(this.dataComponent, this.dataFimComponent, MAX_DATE_SPAN));
    }
    
    @Override
    public void save() {
        WebTestEntity target = this.getMergedEntity();
        WebTestEntityDAO.add(target);
        this.getSession().info(INFO_ENTITY_SAVE_SUCCESS.create().getMessage());
        this.redirectAfterSave();
    }

    @Override
    protected WebTestEntity getMergedEntity() {
        return this.getEntity();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected MessageCreator getHelpTextCreator() {
        return new MockMessageCreator();
    }
}